#ifndef KQUEUE_H_
#define KQUEUE_H_

#ifdef __APPLE__
#include <sys/event.h>
#include "include/event_manager.h"

namespace rpc {

class KQueue : public EventManager {
  public:
    KQueue();
    ~KQueue();

    bool Init();

    virtual bool Add(Event* ev, int flag);
    virtual bool Mod(Event* ev, int flag);
    virtual void Del(Event* ev, int flag);

    void Loop();

  private:
    bool CheckExist(Event* ev) const {
      return ev_set_.count(ev->fd) != 0;
    }
    bool ChangeEvent(Event* ev, uint16 filter, uint16 flag) const;

  private:
    int32 kfd_;
    std::vector<struct kevent> events_;

    typedef std::set<int32> EvSet;
    typedef EvSet::iterator EvIter;
    EvSet ev_set_;

    DISALLOW_COPY_AND_ASSIGN(KQueue);
};

}
#endif /* end for __APPLE__

#endif /* KQUEUE_H_ */
